/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pas.business.data;

import java.io.Serializable;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Transient;
import za.co.pas.business.data.audit.UserType_AUDIT;

/**
 *
 * @author alabuschagne
 */
@Entity
public class UserType extends GenericData implements Serializable
{
    private static final long serialVersionUID = 1L;
    private Integer useLevel = new Integer(99);

    private List<UserType_AUDIT> userTypeAuditList = new LinkedList<UserType_AUDIT>();
    public UserType()
    {
    }

    public UserType(String name, int i)
    {
        setName(name);
        setUseLevel(new Integer(i));
    }

    @Override
    public int hashCode()
    {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserType))
        {
            return false;
        }
        UserType other = (UserType) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    /**
     * @return the useLevel
     */
    public Integer getUseLevel()
    {
        return useLevel;
    }

    /**
     * @param useLevel the useLevel to set
     */
    public void setUseLevel(Integer useLevel)
    {
        this.useLevel = useLevel;
    }

    public boolean check(UserType otherUT)
    {
        if(!super.check(otherUT))
        {
            return false;
        }
        else if(useLevel.compareTo(otherUT.getUseLevel()) != 0)
        {
            return false;
        }
        return true;
    }

    @Override
    public UserType clone()
    {
        UserType ut = new UserType();
        ut.setDescription(description);
        ut.setId(id);
        ut.setName(name);
        ut.setUseLevel(useLevel);
        return ut;
    }

    void set(UserType userType)
    {

    }
private void makeAudit(Company company, String colName, UserLoginDetails uld, Object oldValue, Object newValue)
    {
        UserType_AUDIT ca = new UserType_AUDIT();
        ca.setColName(colName);
        ca.setChangedAt(Calendar.getInstance());
        ca.setChangedBy(uld);
        if (getId() != null)
        {
            ca.setCompany(company);
        }
        else
        {
            ca.setCompany(null);//new
        }
        ca.setLongId(getId());
        if (newValue != null)
        {
            ca.setNewValue(newValue.toString());
        }
        else
        {
            ca.setNewValue(null);
        }

        if (oldValue != null)
        {
            ca.setOldValue(oldValue.toString());
        }
        else
        {
            ca.setOldValue(null);
        }
        add(ca);
    }

    /**
     * @return the userTypeAuditList
     */
    @Transient
    public List<UserType_AUDIT> getUserTypeAuditList()
    {
        return userTypeAuditList;
    }

    /**
     * @param userTypeAuditList the userTypeAuditList to set
     */
    public void setUserTypeAuditList(List<UserType_AUDIT> userTypeAuditList)
    {
        this.userTypeAuditList = userTypeAuditList;
    }

    private void add(UserType_AUDIT ca)
    {
        if(userTypeAuditList == null)
        {
            userTypeAuditList = new LinkedList<UserType_AUDIT>();
        }
        userTypeAuditList.add(ca);
    }

    public String logPrint(int level)
    {
        StringBuilder tab = new StringBuilder();
        for(int i = 0; i < level; i++)
        {
            tab.append("\t");
        }
        StringBuilder sb = new StringBuilder(tab.toString());
        sb.append("USERTYPE\n");
        sb.append(tab.toString());
        sb.append("ID: \t");
        sb.append(id);
        sb.append("\n");
        sb.append(tab.toString());
        sb.append("Name: \t");
        sb.append(name);
        sb.append("\n");
        sb.append(tab.toString());
        sb.append("Desc: \t");
        sb.append(description);
        sb.append("\n");
        sb.append(tab.toString());
        sb.append("Level: \t");
        sb.append(useLevel.toString());
        return sb.toString();
    }
}
